package com.mo.tools.config.redis;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.text.SimpleDateFormat;
import java.util.TimeZone;

/**
 * @name: RedisConfiguration
 * @description: RedisTemplate配置
 * @author: Yang Xingfu
 * @date: 2020-01-08 09:17
 **/
@Configuration
public class RedisConfiguration {

    private static ObjectMapper objectMapper = new ObjectMapper();

    static {
        // FAIL_ON_UNKNOWN_PROPERTIES在序列化的时候，如果遇到不认识的字段的处理方式
        //  默认启用特征，这意味着在遇到未知属性时抛出JsonMappingException. 在引入该特征之前，这是默认设置
        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        //  将抛出一个异常来知名这些是非序列化的类型；如果禁用了，他们将被序列化为空对象，即没有任何属性。
        //  这个特征只对那些没有任何识别注释的“空”bean产生印象（如@json序列化）；那些有注释的bean不会导致抛出异常
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        //  过滤类的属性id
        objectMapper.setFilterProvider(new SimpleFilterProvider().setFailOnUnknownId(false));
        //  在序列化的是，只有那些值为null或被认为为空的值属性才不会被包含在内
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
        //  设置jackson解析时间格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        objectMapper.setDateFormat(simpleDateFormat);
        objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    }

    /**
     * description: redis使用jackson序列化和反序列化
     * @author Yang Xingfu
     * @date 2020/1/8 10:12
     * @param redisConnectionFactory redis连接工厂
     * @return org.springframework.data.redis.core.RedisTemplate<java.lang.Object,java.lang.Object> redis模板
     */
    @Bean("jsonRedisTemplate")
    @ConditionalOnMissingBean(value = RedisConfiguration.class)
    public RedisTemplate<Object, Object> jsonRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        //  Jackson2JsonRedisSerialize序列化器
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        //  stringRedisSerializer序列化器
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

        //  设置key和value的序列化规则
        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }


    /**
     * description:  redis使用jdk序列化和反序列化, 高效二进制存储
     * @author Yang Xingfu
     * @date 2020/1/8 10:14
     * @param redisConnectionFactory redis连接工厂
     * @return org.springframework.data.redis.core.RedisTemplate<byte[],byte[]> redis模板
     */
    @Bean("jdkRedisTemplate")
    @ConditionalOnMissingBean(value = RedisConfiguration.class)
    public RedisTemplate<byte[], byte[]> jdkRedisTemplate (RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<byte[], byte[]> redisTemplate = new RedisTemplate<byte[], byte[]>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        //  jdkRedisSerialize序列化器
        JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();
        //  设置key和value的序列化规则
        redisTemplate.setKeySerializer(jdkSerializationRedisSerializer);
        redisTemplate.setValueSerializer(jdkSerializationRedisSerializer);
        redisTemplate.setHashKeySerializer(jdkSerializationRedisSerializer);
        redisTemplate.setHashValueSerializer(jdkSerializationRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    /**
     * description: redis字符串方式序列化和反序列化
     * @author Yang Xingfu
     * @date 2020/1/8 10:14
     * @param redisConnectionFactory redis连接工厂
     * @return org.springframework.data.redis.core.RedisTemplate<java.lang.String,java.lang.String> redis模板
     */
    @Bean("stringRedisTemplate")
    @ConditionalOnMissingBean(value = RedisConfiguration.class)
    public RedisTemplate<String, String> stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        //  stringRedisSerialize序列化器
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        //  设置key和value的序列化规则
        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setValueSerializer(stringRedisSerializer);
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        redisTemplate.setHashValueSerializer(stringRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    /**
     * description:  redis自定义序列化方式，保持key的可读性，同时保证value效率
     * @author Yang Xingfu
     * @date 2020/1/8 10:44
     * @param redisConnectionFactory redis连接工厂
     * @return org.springframework.data.redis.core.RedisTemplate<java.lang.String,byte[]> redis模板
     */
    @Bean("customRedisTemplate")
    @ConditionalOnMissingBean(value = RedisConfiguration.class)
    public RedisTemplate<String, byte[]> customRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, byte[]> redisTemplate = new RedisTemplate<String, byte[]>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        //  stringRedisSerializer序列化器
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        //  jdkSerializationRedisSerializer序列化器
        JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();
        //  设置key和value的序列化规则
        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setValueSerializer(jdkSerializationRedisSerializer);
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        redisTemplate.setHashValueSerializer(jdkSerializationRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;

    }
}
